import {
	reqGetGoodsInfo,reqAddOrUpdateShopCart
} from '../../api'
import {getUUID} from '../../utils/uuid_token.js'
// actions: 处理异步，书写业务逻辑
const actions = {
	async getGoodInfo({commit}, skuId) {
		let result = await reqGetGoodsInfo(skuId)
		if (result.code == 200) {
			commit('GETGOODINFO', result.data)
		}
	},
	async addOrUpdateShopCart({commit}, {skuId,skuNum}) {
		let result = await reqAddOrUpdateShopCart(skuId,skuNum)
		if(result.code==200){
			return '已经加入购物车'
		}else{
			return Promise.reject(new Error('faile'))
		}
	},
};

// mutations:修改state
const mutations = {
	GETGOODINFO(state, goodInfo) {
		state.goodInfo = goodInfo
		// console.log(goodInfo)
	},

};

//state:存储数据的地方
const state = {
	goodInfo: {},
	uuid_token:getUUID()
};

// getters：相当于计算属性,可以简化数据
const getters = {
	categoryView(state) {
		return state.goodInfo.categoryView || {}
	},
	skuInfo(state) {
		return state.goodInfo.skuInfo || {}
	},
};

//对外暴露Store实例
export default {
	state,
	mutations,
	actions,
	getters
}
